var express = require('express');
var router = express.Router();
const shopDoorService = require("../service/shopDoorService");

//查询所有
router.get("/", async function (req, res) {
    let { type, value, current, size } = req.query;
    let searchObj = {};//
    if (type) {
        if (type == "number") {
            searchObj[type] = value;
        } else {
            searchObj[type] = { $regex: value };
        }
    }
    let data = await shopDoorService.find(searchObj, { current: current || 1, size: size || 10 });
    console.log("dasd", data);
    res.send(data);

})
//根据id查
router.get("/:id", async function (req, res) {
    let id = req.params.id;
    let data = await shopDoorService.findById(id);
    res.send(data)
})
//增加
router.post("/", async function (req, res) {
    let { name,
        phone,
        time,
        address,
        score,
        describe,
        isFlag,
        services,
        adminId,
        commoditys
    } = req.body
    let data = await shopDoorService.insert({
        name,
        phone,
        time,
        address,
        score,
        describe,
        isFlag: isFlag || 0,
        services,
        adminId,
        commoditys,
    });
    // console.log(data);
    res.send(data);
})
//根据id删除
router.delete("/:id", async function (req, res) {
    let id = req.params.id;
    let data = await shopDoorService.delete(id, req.body)
    res.send(data);
})
// 修改
router.put('/:id', async function (req, res, next) {
    let id = req.params.id
    let data = await shopDoorService.update(id, req.body);
    res.send(data)
});
module.exports = router;